package at.fhj.swd.pse.data;

import java.util.List;

import javax.persistence.EntityManager;

import at.fhj.swd.pse.entities.Community;

/**
 * Use this implementation of community DAO to access community objects
 * @author Helmut Kopf
 *
 */

public class CommunityDAOImpl 
	implements CommunityDAO
{
	private final EntityManager m_em;
	
	public CommunityDAOImpl(final EntityManager em) {
		if (em != null) {
			this.m_em = em;
		}
		else {
			throw new NullPointerException("em");
		}
	}

	@Override
	public Community addCommunity(Community community) {
		if (community != null) {
			m_em.persist(community);
			return community;
		}
		else {
			throw new NullPointerException("community");
		}
	}

	@Override
	public Community getCommunity(int id) {
		return (Community)m_em.find(Community.class, id);		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Community> getCommunities() {
		final String hql = "SELECT c FROM Community AS c";  					   
		return m_em.createQuery(hql).getResultList();
	}

	@Override
	public void removeCommunity(Community community) {
		if (community != null) {
			m_em.remove(community);
		}
		else {
			throw new NullPointerException("community");
		}				
	}
	

}
